<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="Expires" content="0">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-control" content="no-cache,no-store,must-revalidate">
    <meta http-equiv="Cache" content="no-cache">
		<title>新增页面</title>
		<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
	</head>
	<body>
		<form class="layui-form" action="" lay-filter="form-filter">
			<div class="mainBox">
				<div class="main-container">

					<div class="layui-form-item">
						<label class="layui-form-label">处置名称</label>
						<div class="layui-input-block">
							<input type="text" name="abHandleItemName" lay-verify="required" autocomplete="off" placeholder="请输入处置名称"
								class="layui-input">
						</div>
					</div>
					
					<!-- 记录方法, 单选-->
					<div class="layui-form-item">
						<label class="layui-form-label">记录方法</label>
						<div class="layui-input-block">
							<!-- <input type="radio" name="abHandleItemContentType" value="text" title="文本" lay-filter="itemtype-filter" > -->
							<input type="radio" name="abHandleItemContentType" value="select" title="被选项" lay-filter="itemtype-filter" checked>

						</div>
					</div>

					<!-- 所属工程,填充 -->
					<div class="layui-form-item">
						<div id="belong2SecD">

						</div>
					</div>
				
				</div>
			</div>
			<div class="bottom">
				<div class="button-container">
					<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
						lay-filter="editAbHandleItem">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
			
				</div>
			</div>
		</form>
		<script src="../../../component/layui/layui.js"></script>
		<script src="../../../component/pear/pear.js"></script>

		<script type="text/javascript">
			// 加载自定义模块,检查浏览器和token
			layui.extend({
        myheader: "{/}/lay-ext/myheader",
      });
			
			layui.use(['form', 'jquery','myheader','layer','laytpl'], function() {
				let form = layui.form;
				let $ = layui.jquery;
				let myheader = layui.myheader;
				let layer = layui.layer;
				let laytpl = layui.laytpl;
				
				var optionData={
					secdlist:[],
					macnolist:[]
				}

        // 定义全局 ajax的请求头
        myheader.ajaxGlobal(myheader.checkToken().tokenValue);

				// 获取父页的数据
				optionData.fuData = layui.sessionData("fu2zi").fuData;
				
				// 清空父传子时临时用的session对象
        layui.sessionData("fu2zi", null);

				getAllSecD()
				
				// 初始化编辑页面				
				initEdit()

				// form 确认上传
				form.on('submit(editAbHandleItem)', function(data) {
					$.ajax({
						url: parent.layui.myheader.baseUrl()+ '/abHandleItem/uptAbHandleItem',
						data: JSON.stringify({
							abHandleItemID: optionData.fuData.abHandleItem.abHandleItemID,
							abHandleItemName: data.field.abHandleItemName,
							abHandleItemContentType: data.field.abHandleItemContentType,
							secDeptID: data.field.belong2SecD
						}),
						contentType: 'application/json',		// 请求时的数据类型
						type: 'post',
						success: function(data) {
							if (data.data==1) {
								layer.msg("已保存", {
									time: 1000
								}, function() {
									parent.layer.close(parent.layer.getFrameIndex(window
										.name)); //关闭当前页
									parent.layui.table.reloadData('abHandleItem-table',{
										scrollPos: 'fixed'  // 保持滚动条位置不变 - v2.7.3 新增
									});
								});
							} else {
								layer.msg("保存失败", {
									time: 1000
								});
							}
						}
					})
					return false;
				});

				function initEdit(){
					//1. 直接赋值
					form.val("form-filter",{
							"abHandleItemID": optionData.fuData.abHandleItem.abHandleItemID,
							"abHandleItemName": optionData.fuData.abHandleItem.abHandleItemName,
							"abHandleItemContentType":optionData.fuData.abHandleItem.abHandleItemContentType,
							"belong2SecD": optionData.fuData.abHandleItem.secDeptID
					})
				}

				// 获取 工程 的 form 模板
				function getAllSecD(){
					// 获取select 数据
					$.ajax(parent.layui.myheader.baseUrl()+"/secDept/getAllSecDeptName",{
						async:false, // 同步方法
						success: function(res){
							if(res.data.length>0){
								optionData.secdlist =[...res.data]
							} else {
								optionData.secdlist =[]
								parent.layer.msg('未获取有效数据')
							}
						}
					})
					
					// 填充layui模板
					let getTpl = optionTpl.innerHTML
					laytpl(getTpl).render(optionData.secdlist,function(html){
						$("#belong2SecD").html(html)
						// form重新渲染select
						form.render('select');
					})
				}
			
			})
		</script>
		<script id="optionTpl" type="text/html">
			<label class="layui-form-label">所属工程</label>
			<div class="layui-input-block">
				<select name="belong2SecD" lay-verify="required" lay-filter="belong2SecD-filter">
					<option value=""></option>
					{{# layui.each(d,function(index,item){ }}
						<option value="{{item.secDeptID}}">{{item.secDeptName}} </option>
					{{# }); }}		
				</select>
			</div>
		</script>

	</body>
</html>
